package thread;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import util.Tools;

public class StatisName {

    public static void main(String[] args) throws Exception {
        String path = System.getProperty("user.dir");
        List<String> nameList = Tools.readFile(path
                + "/src/testFile/hlm/hongloumeng.txt");
        int count = nameList.size();
        int rate = 10000;
        int cur = count / rate;
        int las = count % rate;
        CountDownLatch countDown = new CountDownLatch(cur + (las <= 0 ? 0 : 1));
        List<String> cList = new ArrayList<String>();
        long startTime = Calendar.getInstance().getTimeInMillis();
        for (int i = 1; i <= cur; i++) {
            if (i == 1) {
                cList = nameList.subList(0, rate);
            } else {
                cList = nameList.subList((i - 1) * rate, i * rate);
            }
            Thread t = new Thread(new StatisNameHandler(cList, countDown));
            t.start();
        }
        if (las > 0) {
            cList = nameList.subList((cur * rate), (cur * rate) + las);
            Thread t = new Thread(new StatisNameHandler(cList, countDown));
            t.start();
        }
        countDown.await();
        long endTime = Calendar.getInstance().getTimeInMillis();
        System.out.println(Tools.userName.toString());
        System.out.println("开始时间: " + startTime);
        System.out.println("结束时间: " + endTime);
        System.out.println("使用时间(毫秒数)：" + (endTime - startTime));
    }
}
